package github.pop.ares.block;

import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.play.server.SUpdateTileEntityPacket;
import net.minecraft.tileentity.ITickableTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityType;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.SoundEvent;
import net.minecraft.util.SoundEvents;
import net.minecraft.world.World;
import net.minecraftforge.common.util.Constants;

import javax.annotation.Nullable;

/**
 * data syc
 */
public class ObsidianZombieTileEntity extends TileEntity implements ITickableTileEntity {

    private boolean flag = false;
    private int MAX_TIME = 5*20;
    private int timer = 0;

    public ObsidianZombieTileEntity() {
        super(TileEntityTypeRegistry.obsidianZombieTileEntity.get());
    }

    /**
     *
     */
    @Override
    public void tick() {
        World world = this.level;
        boolean isRemote = !world.isClientSide;
        if(isRemote&&flag){
            // if at client side
            // play the voice
            final PlayerEntity nearestPlayer = world.getNearestPlayer(worldPosition.getX(),
                    worldPosition.getY(), worldPosition.getZ(), 10, false);
            world.playSound(nearestPlayer,worldPosition,
                    SoundEvents.ZOMBIE_AMBIENT, SoundCategory.AMBIENT, 1.0f, 1.0f);
        }
        if(!isRemote){
            if(timer == MAX_TIME){
                // data change at then server side
                flag = true;
                // if you need trigger data sync
                // you can call this method
                world.sendBlockUpdated(worldPosition,
                        getBlockState(), getBlockState(), Constants.BlockFlags.BLOCK_UPDATE);
                flag = true;
                timer = 0;
            }
            timer ++;
        }
    }

    @Nullable
    @Override
    public SUpdateTileEntityPacket getUpdatePacket() {
        // server send data method
        // wrapper data send
        // getUpdateTag is NBT data
        // second arg is serialize code, custom write
        return new SUpdateTileEntityPacket(worldPosition,
                1,getUpdateTag());
    }

    @Override
    public void onDataPacket(NetworkManager net, SUpdateTileEntityPacket pkt) {
        // client receive data method
        handleUpdateTag(level.getBlockState(pkt.getPos()),
                pkt.getTag());
    }

    /*
    * method getUpdateTag and handleUpdateTag
    * when block loading call
    *
    * for sync you block data,
    * you need block loading
    * implements getUpdatePackage and onDataPackage
    * and call getUpdateTag and handleUpdateTag
    * */

    @Override
    public CompoundNBT getUpdateTag() {

        CompoundNBT nbt = super.getUpdateTag();
        nbt.putBoolean("flag",flag);
        return nbt;
    }

    @Override
    public void handleUpdateTag(BlockState state, CompoundNBT tag) {
        flag = tag.getBoolean("flag");
    }
}
